package com.rocbrid.order.exception;

import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rocbrid.common.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
@Component
public class MyBlockException implements BlockExceptionHandler {
    private ObjectMapper objectMapper= new ObjectMapper(); //将对象转化为json
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s, BlockException e) throws Exception {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter writer = httpServletResponse.getWriter();

        R error = R.error(s + "被sentinel限制了,原因是" + e.getClass());
        String s1 = objectMapper.writeValueAsString(error);//将对象转化为json
        writer.write(s1);

        writer.flush();
        writer.close();
    }
}
